$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/salmb/addSalmb.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$('#updateForm').submit(function() {
		$.ajax({
			url: '/wms/salmb/updateSalmb.xhtml',
			data: $('#updateForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("更新成功!");
					closeDialogUpdate();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	optFormater();
	ajaxTable();
	setDialogAdd();
	closeDialogAdd();
	setDialogUpdate();
	closeDialogUpdate();
});
function ajaxTable() {
	$('#salmbTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			iconCls : 'icon-add',
			handler : function() {
				if(globalMaps.get('3') != undefined&&(maac=='N'||maac=='S')) {
					openDialogAdd();
				}
			}
		}, '-', {
			id : 'toolbarSearch',
			iconCls : 'icon-remove',
			handler : function() {
				if(globalMaps.get('3') != undefined&&(maac=='N'||maac=='S')) {
					delSalmb();
				}
			}
		}
		],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
		},
		onClickRow : function(rowIndex, rowData) {
			$('#salmbTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			if(globalMaps.get('3') != undefined&&(maac=='N'||maac=='S')) {
				updateSalmbSet(rowData.mbac,rowData.mbad,rowData.mbae,rowData.mbaf,rowData.mbag,rowData.mbah,rowData.mbai,rowData.mbaj,rowData.mbak,rowData.mbal,rowData.mban,rowData.mbao,rowData.mbap);
			}
	    }
	}).datagrid('acceptChanges');
}
function reloadTable() {
	$('#salmbTable').datagrid('reload');
	$('#salmbTable').datagrid('clearSelections');
}
function optFormater() {
	var update = '';
	if(globalMaps.get('4') != undefined && maac == 'N') {
		update += "<a href='#' onclick='deleteSalmamb(\""+maaa+"\",\""+maab+"\")' class='easyui-linkbutton' plain='true' iconCls='icon-cancel'>删除</a>";
	}
	if(globalMaps.get('5') != undefined && maac == 'N') {
		update += "<a href='#' onclick='auditSalmamb(\""+maaa+"\",\""+maab+"\",\"Y\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>审核</a>";
	}
	if(globalMaps.get('6') != undefined && maac == 'Y') {
		update += "<a href='#' onclick='auditSalmamb(\""+maaa+"\",\""+maab+"\",\"C\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>指定结束</a>";
		update += "<a href='#' onclick='auditSalmamb(\""+maaa+"\",\""+maab+"\",\"N\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>撤销审核</a>";
	}
	if(globalMaps.get('6') != undefined && maac == 'C') {
		update += "<a href='#' onclick='auditSalmamb(\""+maaa+"\",\""+maab+"\",\"Y\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>撤销结束</a>";
	}
	if(globalMaps.get('7') != undefined && maac == 'Y') {
		update += "<a href='#' onclick='auditSalmamb(\""+maaa+"\",\""+maab+"\",\"V\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>作废</a>";
	}
	if(globalMaps.get('3') != undefined && maac == 'S') {
		update += "<a href='#' onclick='auditSalmamb(\""+maaa+"\",\""+maab+"\",\"N\")' class='easyui-linkbutton' plain='true' iconCls='icon-ok'>保存</a>";
	}
	$("#menuDiv").html(update);
	$.parser.parse($("#menuDiv"));
}
//----------------新增Begin
function setDialogAdd() {
	$('#salmbAdd').dialog({
		title : '新增单身',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogAdd() {
	addSalmbReset();
	$('#salmbAdd').dialog('open');
}
function closeDialogAdd() {
	addSalmbReset();
	$('#salmbAdd').dialog('close');
}
function salmbAdd() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
function addSalmbReset() {
	$("#salmbAddMbad").val("");
	$("#salmbAddMbae").val("");
	$("#salmbAddMbaf").val("");
	$("#salmbAddMbag").val("");
	$("#salmbAddMbah").val("");
	$("#salmbAddMbai").text("");
	$("#salmbAddMbaj").text("");
	$("#salmbAddMbak").val("");
	$("#salmbAddMbal").val("");
	$("#salmbAddMban").val("");
	$("#salmbAddMbao").val("");
	$("#salmbAddMbap").val("");
}
//----------------新增End
//----------------更新Begin
function setDialogUpdate() {
	$('#salmbUpdate').dialog({
		title : '编辑',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogUpdate() {
	$('#salmbUpdate').dialog('open');
}
function closeDialogUpdate() {
	updateSalmbReset();
	$('#salmbUpdate').dialog('close');
}
function salmbUpdate() {
	var validateResult = $("#updateForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#updateForm').submit();
}
function updateSalmbReset() {
	$("#salmbUpdateMbad").val("");
	$("#salmbUpdateMbae").val("");
	$("#salmbUpdateMbaf").val("");
	$("#salmbUpdateMbag").val("");
	$("#salmbUpdateMbah").val("");
	$("#salmbUpdateMbai").text("");
	$("#salmbUpdateMbaj").text("");
	$("#salmbUpdateMbak").val("");
	$("#salmbUpdateMbal").val("");
	$("#salmbUpdateMban").val("");
	$("#salmbUpdateMbao").val("");
	$("#salmbUpdateMbap").val("");
}
function updateSalmbSet(mbac,mbad,mbae,mbaf,mbag,mbah,mbai,mbaj,mbak,mbal,mban,mbao,mbap) {
	openDialogUpdate();
	$("#salmbUpdateMbac").val(mbac);
	$("#salmbUpdateMbad").val(mbad);
	$("#salmbUpdateMbae").val(mbae);
	$("#salmbUpdateMbaf").val(mbaf);
	$("#salmbUpdateMbag").val(mbag);
	$("#salmbUpdateMbah").val(mbah);
	$("#salmbUpdateMbai").text(mbai);
	$("#salmbUpdateMbaj").text(mbaj);
	$("#salmbUpdateMbak").val(mbak);
	$("#salmbUpdateMbal").val(mbal);
	$("#salmbUpdateMban").val(mban);
	$("#salmbUpdateMbao").val(mbao);
	$("#salmbUpdateMbap").val(mbap);
}
//----------------更新End
//----------------删除Begin
function delSalmb(){
	if ($('#salmbTable').datagrid('getSelected')) {
		var ids = [];
		var selectedRow = $('#salmbTable').datagrid('getSelections');
		for ( var i = 0; i < selectedRow.length; i++) {
			ids.push(selectedRow[i].mbac);
		}
		var mbac = ids.join(',');
		changeStatus('/wms/salmb/delSalmb.xhtml?mbac=' + mbac+'&mbaa='+maaa+'&mbab='+maab);
		$('#salmbTable').datagrid('clearSelections');
	}
}
function delSalmbAll(){
	changeStatus('/wms/salmb/delSalmbAll.xhtml?mbaa='+maaa+'&mbab='+maab);
}
//----------------删除End
//-------------SHENHE---
function auditSalmamb(maaa,maab,flag) {
	changeStatus('/wms/salmb/auditSalmb.xhtml?maaa=' + maaa + '&maab=' + maab+ '&flag=' + flag);
	window.parent.returnValue=1;
	window.close();

}
function deleteSalmamb(maaa,maab) {
	
	
	$.messager.confirm('删除提示', '你确定删除该采购单吗?', function(r) {
		if(r) {
			changeStatus('/wms/salmb/delSalmamb.xhtml?maaa=' + maaa + '&maab=' + maab);
			window.parent.returnValue=1;
			window.close();
		}
	});
}
function saveSalmamb(){
	var validateResult = $("#headForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#headForm').submit();
}
function changeStatus(url){
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		dataType : "text",
		url : url,
		error : function () {
			alert('请求失败');
		},
		success : function(result){
			if(result=="success") {
//				alert("操作成功!");
			} else {
				alert(result);
			}
			reloadTable();
		}
	});
}
window.onbeforeunload = function(){  
	var n = window.event.screenX - window.screenLeft;   
    var b = n > document.documentElement.scrollWidth-20;   //		
      if(b&&window.event.clientY<0||window.event.altKey){   
    	  delSalmbAll();
//          alert("是关闭而非刷新");   
//          window.event.returnValue   =   "是否关闭？";
      }else{
//             alert("是刷新而非关闭");   
     }  
 } ;

